package com.jxchexie.admin;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.jxchexie.api.Api;
import com.jxchexie.signin.R;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.rengwuxian.materialedittext.MaterialEditText;

import org.json.JSONObject;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

public class modifyNeededInfo extends AppCompatActivity {

    private String str_info1;
    private String str_info2;
    private String str_info3;
    private String str_info4;
    private Button mBtn;

    private MaterialEditText info1;
    private MaterialEditText info2;
    private MaterialEditText info3;
    private MaterialEditText info4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_needed_info);
        init();
        upLoad();
    }

    private void upLoad() {
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                str_info1 = String.valueOf(info1.getText());
                str_info2 = info2.getText().toString();
                str_info3 = info3.getText().toString();
                str_info4 = info4.getText().toString();
                if(str_info1.equals("") || str_info2.equals("") || str_info3.equals("") || str_info4.equals("")){
                    Toast.makeText(modifyNeededInfo.this,"需要修改的每一条都不能为空！",Toast.LENGTH_SHORT).show();
                }else {
                    modify();
                }

            }
        });
    }

    private void modify() {
        /*显示提示正在登录对话框*/
        final MaterialDialog md=new MaterialDialog.Builder(modifyNeededInfo.this)
                .title("提示")
                .content("正在上传中...")
                .progress(true, 0)
                .show();

        /*构造请求体*/
        HashMap<String,String>params = new HashMap<>();
        params.put("A", str_info1);
        params.put("B", str_info2);
        params.put("C", str_info3);
        params.put("D", str_info4);
        JSONObject jsonObject = new JSONObject(params);
        OkGo.post(Api.Set_Info)
                .tag(this)//
                .upJson(jsonObject.toString())//
                .execute(new StringCallback() {

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        md.dismiss();
                        Toast.makeText(modifyNeededInfo.this,"已传递需要的签到信息",Toast.LENGTH_LONG).show();
                    }
                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        md.dismiss();
                        Toast.makeText(modifyNeededInfo.this,"连接服务器失败",Toast.LENGTH_LONG).show();
                    }
                });
    }

    private void init() {
        info1 = (MaterialEditText) findViewById(R.id.A);
        info2 = (MaterialEditText) findViewById(R.id.B);
        info3 = (MaterialEditText) findViewById(R.id.C);
        info4 = (MaterialEditText) findViewById(R.id.D);
        mBtn = (Button) findViewById(R.id.mModify);
    }
}
